#ifndef HWDDECODETHREAD_H
#define HWDDECODETHREAD_H

#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#define UINT64_C
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/frame.h>
#include <libavutil/samplefmt.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libavutil/parseutils.h>
#include <libavutil/error.h>
#include <libavutil/buffer.h>
//#include <libavutil/hwcontext.h>
//#include <libavutil/hwcontext_qsv.h>
#include <libavutil/mem.h>
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
}
#endif

#include <QThread>
#include <QImage>
#include <QObject>

class HwdDecodeThread : public QThread
{
     Q_OBJECT
public:
    explicit HwdDecodeThread();
    ~HwdDecodeThread();

    void set_path(QString url);

    void ffmpeg_init_variables();
    void ffmpeg_free_variables();
    bool open_input_file();
    static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
                                            const enum AVPixelFormat *pix_fmts);
    static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type);

    void stop_thread();

protected:
    void run();

signals:
    void send_image(const QImage &img);

private:
    AVFormatContext *mFmtCtx       =NULL;
    const AVCodec   *mVideoCodec   =NULL;
    AVCodecContext  *mVideoCodecCtx=NULL;
    AVPacket        *mPkt          = NULL;
    AVFrame         *mYUVFrame     = NULL;
    AVFrame         *mRGBFrame     = NULL;
    AVFrame         *mNV12Frame    = NULL;
    AVStream        *mVideoStream  = NULL;

    uchar *mOutBuffer;
    struct SwsContext *mImgCtx=NULL;

    QString mFilePath;

    int mVideoStreamIndex =-1;
    int mNumBytes = -1;

    int mRet =0;

    bool mInitFlag=false,mOpenFlag=false,mStopFlag=false;
};

#endif // HWDDECODETHREAD_H
